<template>
  <div>
    <el-row :gutter="24">
      <el-col :span="8">键值</el-col>
      <el-col :span="8">中文</el-col>
      <el-col :span="8">
        {{ langCn }}
        <el-select v-model="languageType" style="width: 160px" placeholder="选择要翻译的语言" @change="batchTranslate">
          <el-option
            v-for="item in languageList"
            :key="item.id"
            :label="item.langName"
            :value="item.langType">
          </el-option>
        </el-select>
        <el-button style="margin-left: 10px" size="mini" type="primary" @click="getNullLang">仅显示空值</el-button>
        <el-button type="primary" plain size="mini" @click="batchInsert">批量保存</el-button>
      </el-col>
    </el-row>
    <el-row :gutter="24" v-for="(row, index) in translateList" :key="index">
      <el-col :span="8">
        <el-input v-model="row.keyCode" readonly></el-input>
      </el-col>
      <el-col :span="8">
        <el-input v-model="row.chinese" readonly></el-input>
      </el-col>
      <el-col :span="8">
        <el-input v-model="row.language"></el-input>
      </el-col>
    </el-row>
<!--    <el-table v-loading="loading" stripe :data="translateList" @selection-change="handleSelectionChange">-->
<!--      <el-table-column label="中文" align="center" prop="chinese" />-->
<!--      <el-table-column label="外语" align="center" prop="language" />-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--    </el-table>-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="2" :offset="22">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="batchInsert"
        >批量保存</el-button>
      </el-col>
    </el-row>

  </div>
</template>

<script>
import {
  listTranslate,
  getTranslate,
  delTranslate,
  addTranslate,
  updateTranslate,
  batchInsert,
  batchTranslate,
  listTranslateApi
} from "@/api/video/translate";
import {listLang} from "@/api/video/lang";

export default {
  name: "Translate",
  watch:{
    someValue: {
      handler (newVal, oldVal) {
        if(newVal != oldVal) {
          this.getList(newVal)
        }
      }
    }
  },
  props: {
    someValue: {
      type: String,
      default: '0'
    },
    langCn: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 语言对照表格数据
      translateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        langId: null,
        chinese: null,
        language: null,
        status: ''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      langList: [],
      languageType: '',
      visible: false,
      languageList: [],
    };
  },
  created() {
    this.listTranslateApi();
    // this.getList();
  },
  mounted() {
    // this.getList();
  },
  methods: {
    listTranslateApi()  {
      listTranslateApi().then(response => {
        this.languageList = response.rows;
      });
    },
    getNullLang() {
      if(this.translateList.length > 0) {
        this.queryParams.status = '1';
      }
      this.getList(this.someValue);
    },
    /** 查询语言对照列表 */
    getList(langId) {
      this.loading = true;
      if(langId) {
        this.queryParams.langId = langId
      }
      listTranslate(this.queryParams).then(response => {
        this.translateList = response.rows;
        this.queryParams.status = '';
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        langId: null,
        chinese: null,
        language: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList(this.someValue);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加语言对照";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTranslate(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改语言对照";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTranslate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList(this.someValue);
            });
          } else {
            addTranslate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList(this.someValue);
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除语言对照编号为"' + ids + '"的数据项？').then(function() {
        return delTranslate(ids);
      }).then(() => {
        this.getList(this.someValue);
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/translate/export', {
        ...this.queryParams
      }, `translate_${new Date().getTime()}.xlsx`)
    },
    // async batchList() {
    //   let num = 0;
    //   const list = [];
    //   for (let i = 0; i < this.translateList.length; i++) {
    //     if(this.translateList[i].language == null || this.translateList[i].language == "") {
    //       list.push(this.translateList[i]);
    //       num++;
    //     }
    //   }
    //   await this.handleBatchUpdate(list);
    // },
    batchInsert() {
      batchInsert(this.translateList).then(response => {
        this.$modal.msgSuccess("保存成功");
        this.open = false;
        this.getList(this.someValue);
      }).catch(() => {});
    },
    batchTranslate() {
      this.translateList[0].languageType = this.languageType;
      let loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      batchTranslate(this.translateList).then(response => {
        this.translateList = response.data;
        this.visible = false;
        loading.close();
      }).catch((e) => {
        this.$modal.msgError(e)
        this.visible = false;
        loading.close();
      });
    }
  }
};
</script>
<style>
.el-row {
  margin-bottom: 10px;
}
</style>
